/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.infoq.bambook
import groovy.xml.MarkupBuilder
import org.custommonkey.xmlunit.*

def bambook_dir = "infoq_bambook"
def timestamp = new Date().format('yyyyMMddHHmmss')

def void createFolders(){
    def images = new File(".\\infoq_bambook\\snbc\\images")
    images.mkdirs()
    def snbf = new File(".\\infoq_bambook\\snbf")
    snbf.mkdirs()
}
createFolders()

// (new File(".\\infoq.snb")).delete();

def void snbc4EachContent(_title, _content, nodeName){
    
    def writer = new FileWriter(".\\infoq_bambook\\snbc\\"+ nodeName +".snbc")
    def builder = new groovy.xml.StreamingMarkupBuilder()
    builder.encoding = "UTF-8"
    def snbc = {
        mkp.xmlDeclaration()
        snbc() {
            head() {
                title(){
                    mkp.yieldUnescaped("<![CDATA[${_title}]]>")
                }
            }
            body() {
                _content.each{ p ->
                    text(){
                        mkp.yieldUnescaped("<![CDATA[${p.text()}]]>")
                    }
                    text(){
                        mkp.yieldUnescaped("<![CDATA[ ]]>")
                    }
                }
            }
        }
    }
    writer << builder.bind(snbc)
    writer.close()
}

def void snbfBook(timestamp){
    def writer = new FileWriter(".\\infoq_bambook\\snbf\\book.snbf")
    def builder = new groovy.xml.StreamingMarkupBuilder()
    builder.encoding = "UTF-8"
    def snbf = {
        mkp.xmlDeclaration()
        'book-snbf'(version:'1.0') {
            head() {
                name(){
                    mkp.yieldUnescaped("<![CDATA[InfoQ中文站"+timestamp+"]]>")
                }
                author(){
                    mkp.yieldUnescaped("<![CDATA[InfoQ中文站]]>")
                }
                language('ZH-CN')
                rights(){
                    mkp.yieldUnescaped("<![CDATA[InfoQ中文站]]>")
                }
                publisher(){
                    mkp.yieldUnescaped("<![CDATA[InfoQ中文站]]>")
                }
                generator(){
                    mkp.yieldUnescaped("<![CDATA[Kai Feng Zhang]]>")
                }
                created(){
                    new Date()
                }
                    'abstract'(){
                    mkp.yieldUnescaped("<![CDATA[InfoQ中文站最新内容]]>")
                }
                cover()
            }
        }
    }
    writer << builder.bind(snbf)
    writer.close()
}

def void snbfToc(tocMap){

    def writer = new FileWriter(".\\infoq_bambook\\snbf\\toc.snbf")
    def builder = new groovy.xml.StreamingMarkupBuilder()
    builder.encoding = "UTF-8"

    def keys = tocMap.keySet();

    def snbf = {
        mkp.xmlDeclaration()
        'toc-snbf'() {
            head() {
                chapters(tocMap.size())
            }
            body() {
                keys.each{ nodeName ->
                    chapter(src:"${nodeName}.snbc"){
                        mkp.yieldUnescaped("<![CDATA[${tocMap.get(nodeName)}]]>")
                    }
                }
            }
        }
    }
    writer << builder.bind(snbf)
    writer.close()
}


def url_cn = "http://www.infoq.com/cn/rss/rss.action?token=17PFUJbbXetybUaZi8qPWIo707xXRybK"
def xml = new XmlSlurper().parse(url_cn);
def xml_slurper = new XmlSlurper(new org.cyberneko.html.parsers.SAXParser())

def tocMap = new LinkedHashMap()

xml.item.each{ it->

    def title =  it.title.text();
    def link = it.link.text()
    def nodeName = link.substring(link.lastIndexOf('/')+1)
    println title+"," + link

    if(link.contains("/articles/") || link.contains("/news/")){
        try{
            def page = xml_slurper.parse(link)
            def content = page.depthFirst().grep{ it.name() == 'P' && (
                (it.parent().@class == 'box-content-5' && it.@class != 'info') ||
                (it.parent().parent().@class == 'box-content-5' && it.parent().@class != 'vendor-content-box' && it.parent().@class != 'comments-header' && it.parent().@class != 'vendor-content-box-float') ||
                (it.parent().parent().parent().@class == 'box-content-5' && it.@class != 'comment-footer' && it.parent().parent().@class != 'vendor-content-box' && it.parent().parent().@class != 'comments-header'))}
            snbc4EachContent(title, content, nodeName)
            tocMap.put(nodeName, title);
        }catch(Exception e){
        }
    }
}

snbfToc(tocMap)
snbfBook(timestamp)

try {
    Thread.sleep(5000);
} catch (InterruptedException ex) {
}

def bookName = "infoq"+timestamp+".snb"
InfoQBambookHelper.pack(bookName)
InfoQBambookHelper.upload(bookName)